package com.csdj.t309.geamtest.service;

import com.csdj.t309.geamtest.common.CacheConstant;
import com.csdj.t309.geamtest.entity.Blindbox;
import com.csdj.t309.geamtest.entity.Jifen;
import com.csdj.t309.geamtest.entity.Jigsaw;
import com.csdj.t309.geamtest.entity.User;
import com.csdj.t309.geamtest.mapper.BlindboxMapper;
import com.csdj.t309.geamtest.mapper.JifenMapper;
import com.csdj.t309.geamtest.mapper.JigsawMapper;
import com.csdj.t309.geamtest.mapper.UserMapper;
import com.csdj.t309.geamtest.service.IJigsawService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 田港
 * @since 2022-01-12
 */
@Service
public class JigsawService extends ServiceImpl<JigsawMapper, Jigsaw> implements IJigsawService {
    @Resource
    private JigsawMapper jigsawMapper;
    @Resource
    private JifenMapper jifenMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private BlindboxMapper blindboxMapper;

    @Override
    public List<Jigsaw> selJS() {
        HashMap<String, Object> map = new HashMap<>();
        List<Jigsaw> js = jigsawMapper.selectByMap(map);
        if (js.size() > 0) {
            return js;
        }
        return null;
    }

    @Override
    public Jigsaw selMangHe(Integer id) {
        Jigsaw jigsaw = jigsawMapper.selectById(id);
        if (jigsaw != null) {
            return jigsaw;
        }
        return null;
    }

    @Override
    public List suiJiShu(Integer cishu, Integer jsId,Integer id) {
        User user = userMapper.selectById(id);
        Integer[] index = new Integer[10];
        Integer num = 0;
        Integer jsNum = selMangHe(jsId).getJsNum();
        List<Integer> list = new ArrayList<Integer>();
        List<Integer> zhanshi = new ArrayList<Integer>();
        if(jsNum>cishu){
            Jigsaw jifena = new Jigsaw();
            jifena.setId(jsId);
            jifena.setJsNum(jsNum-cishu);
            jigsawMapper.updateById(jifena);
            for (Integer i = 0; i < cishu; i++) {
                String ZiMu = "012345678";
                Random random = new Random();
                index[i] = random.nextInt(ZiMu.length());
                num += 10;
            }
            for (int i=0; i<index.length; i++){
                if(!list.contains(index[i])){
                    list.add(index[i]);
                }
                if(!zhanshi.contains(index[i])){
                    zhanshi.add(index[i]);
                }
            }
            if (user != null) {
                Integer integrate = user.getIntegrate();
                Jifen jifen = new Jifen();
                jifen.setJiUserId(id);
                jifen.setJiOperateType("抽奖");
                Date date = new Date();
                jifen.setJiOperateDate(date);
                jifen.setJiNum("-" + num);
                jifen.setJiRemainder(integrate - num);
                jifenMapper.insert(jifen);
                if(user.getIntegrate()>integrate){
                    User user1 = new User();
                    user1.setId(id);
                    user1.setIntegrate(integrate - num);
                    userMapper.updateById(user1);
                }else {
                    return null;
                }

            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("bb_user_id",id);
            map.put("bb_js_id",jsId);
            List<Blindbox> blindbox1 = blindboxMapper.selectByMap(map);
            List<Integer> bliahao = new ArrayList<Integer>();
            for(Blindbox bian : blindbox1){
                bliahao.add(bian.getBbBianHao());
            }
            zhanshi.removeAll(bliahao);
            for(Integer bians : zhanshi){
                Blindbox blindbox = new Blindbox();
                blindbox.setBbUserId(id);
                blindbox.setBbBianHao(bians);
                blindbox.setBbJsId(jsId);
                blindboxMapper.insert(blindbox);
            }
            return list;
        }else {
            list.add(10);
            return list;
        }
    }
}
